package com.javatom.notes.mongo.service;

import com.javatom.notes.mongo.po.TreePojo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

/**
 * 〈功能概述〉<br>
 *
 * @className: MongoTreeOperatService
 * @package: com.javatom.notes.mongo.service
 * @author: TR
 * @date: 2019/7/10 9:59
 */
@Service
@Slf4j
public class MongoTreeOperatService {
    @Autowired
    private MongoTemplate mongoTemplate;
    public void initData() {
        List<TreePojo> treePojos=new ArrayList<TreePojo>();

        for (int i=0;i<5;i++){
            TreePojo treePojo = new TreePojo();
            treePojo.setTableName("我是表明"+1);
            treePojo.setId(i);
            treePojo.setBusinessId("businessId"+i);
            List<TreePojo> treePojos1=new ArrayList<TreePojo>();
            for (int j=0;j<5;j++){
                TreePojo treePojo1 = new TreePojo();
                treePojo1.setTableName("我是弟二个个"+1);
                treePojo1.setId(3000*(i+1)+j);
                treePojo1.setBusinessId("businessId"+1000+j);
                List<TreePojo> treePojos2=new ArrayList<TreePojo>();
                for (int c=0;c<5;c++){
                    TreePojo treePojo2 = new TreePojo();
                    treePojo2.setTableName("表明3"+1);
                    treePojo2.setId(5000*(j+1)+c);
                    treePojo2.setBusinessId("businessId"+2000+j);
                    treePojos2.add(treePojo2);
                }
                treePojo1.setNodes(treePojos2);
                treePojos1.add(treePojo1);
            }
            treePojo.setNodes(treePojos1);
            treePojos.add(treePojo);
        }

        mongoTemplate.insert(treePojos, TreePojo.class);

    }

    public void testFind(){
        Query query=new Query();
        Criteria criteria=Criteria.where("nodes.id").is(3000);
        query.addCriteria(criteria);
        List<TreePojo> treePojos = mongoTemplate.find(query, TreePojo.class);
  log.error("pp {}",treePojos);
    }
}